const gulp = require('gulp')
const { series, parallel } = gulp
const fs = require('fs')
const NodeRSA = require('node-rsa')
const md5 = require('js-md5')
const _ = require('lodash')
var replace = require('gulp-replace')
var rename = require("gulp-rename");
var uglify = require('gulp-uglify');
var pump = require('pump');
const babel = require('gulp-babel');

function build_license(cb) {
    // const userkey = new NodeRSA({b: 1024});
    // let user_priKey =  userkey.exportKey('pkcs8-private-pem');
    // let user_pubKey =  userkey.exportKey('pkcs8-public-pem');

    // const licenseKey = '\
    // -----BEGIN PRIVATE KEY-----\
    // MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIcTwSyRqekRaopV\
    // e0hRRrPkyrSQH6svv6IcctF4yGxiExNxus84xJLoesZFe+N8XY91hBJLn+izSpFW\
    // sInZztS3tobfef2VzN9WHQZMuGJk0aTFMnlGhc1mnbFtH1cuJP5IJ27qA1ZQV8PU\
    // EM5GH39HrgYRHX0EFd4Q6LB2Qx7bAgMBAAECgYBolE7XZt+Z5zYJbC1KzEJlPRoD\
    // uiIKs6k2pbfvngfugfAq+b6ceWMYKo4DvAAzHDKDw4RBdfOUgluVMvzzfYK32cu8\
    // Mhks8eauNVubIE279VX2yjTLH/RFqaLPmVIfRGs2bbS1SZKxCZKmjfnbv3Ma5EKG\
    // u3q41hMc5Ix0tOwuQQJBAPpkmOqJN1M6bn11BrxOtW3A/fBByMySzp/XnUZrBmrx\
    // 1xk/2vK5IC2PYlglzKYd7ES6662/GM4eDTS2glif4qMCQQCKGhkTNKD9ZHJwwPCh\
    // YYVk7CSe/2UTP+C8rkIvIaemgniw4GswLl5Gcde5JWHXywhUJu8MexZyu/aZ1UJP\
    // cM5pAkBA1g+qm4FR8c1uewEVfaQJGXIn5kDiiBks1p4I/+mXg6vZA+nj8//bDEfA\
    // 33jQgusNGQGx6cu4g5uTzLaP0EbVAkAK5gxci6a42Jh/dXqIAd45HLm3IoijgEMI\
    // IOhwN8O3TEyiCNPstXwTKyDPJ7gVA5zgp0HuRjh7TYIkmcPl6Q0hAkEA7RkS6NIJ\
    // 6l9vJyI7lHzESmsukmDTe7S0YvIMKuQsIEW2Bb/q9InPwuavtNqCYSku4gt3u4lQ\
    // W9aYjUOdNZWZPA==\
    // -----END PRIVATE KEY-----'

    // let enkey = new NodeRSA(user_priKey, 'pkcs8-private-pem')

    // // `${fncode}---${mac}|||${_version.hash}|||${createTime}|||${expirationTime}`

    // let fncode = fs.readFileSync('./src/fncode.js', 'utf8')

    // var info = `${fncode}@@@78:ca:39:b0:e6:7d|||13b5e9dbf91d94a6f4c96fb71dd343c4|||2018-11-11|||2020-11-11`

    // info = enkey.encryptPrivate(info,'base64')

    // const key_license = new NodeRSA()
    // key_license.importKey(licenseKey, 'pkcs8-private-pem')
    // let license = key_license.encryptPrivate(`${info}|||${user_pubKey}`,'base64')
    // fs.writeFile("../license.txt", license, function(err) {
    //     
    // });
    // )
    // cb();
}

function bind_license(cb) {
    // let fncode = fs.readFileSync('./src/fncode.js', 'utf8')
    // let md5value = md5(fncode)
    // md5value = md5(`${md5value}--emidraw-ss`)
    // md5value = _.chunk(md5value, 4)
    // let hash = []
    // md5value.forEach((item)=>{
    //     hash.push(`_${item.join("")}`)
    // })
    // 
    // gulp.src('./src/store/actions-src.js')
    // .pipe(replace(/addNode\(/, '[window.codehash[0]]('))
    // .pipe(replace(/delNode\(/, '[window.codehash[1]]('))
    // .pipe(replace(/nodeUpdateByPath\(/, '[window.codehash[2]]('))
    // .pipe(replace(/currentNodeChange\(/, '[window.codehash[3]]('))
    // .pipe(replace(/currentNodeUpdate\(/, '[window.codehash[4]]('))
    // .pipe(replace(/UpdateCurrentHandle\(/, '[window.codehash[5]]('))
    // .pipe(replace(/operationModeChange\(/, '[window.codehash[6]]('))
    // .pipe(replace(/setCurrentDom\(/, '[window.codehash[7]]('))
    
    // .pipe(rename("./src/store/actions.js"))
    // .pipe(gulp.dest('./'));
    // cb();
}

function compress_fncode(cb) {
    pump([
            gulp.src('./src/fncode-src.js'),
            babel({
                "presets": ["env"]
            }),
            uglify(),
            rename("fncode-test.js"),
            gulp.dest('./src')
        ],
        cb
    );
}


exports.buildlicense = build_license
exports.compressfncode = compress_fncode

exports.default = parallel(compress_fncode);